This script uses the construction method to dynamically create an array of
link
objects (which have two properties, URL
and description
).
It uses the array.length
property, so the script user doesn't have to worry about the
number of elements or finding all the locations of specific constants.
The script redefines the toString
method for the link
object to
make the document.write
calls easier to use.
It creates an array of randomValues to be used as an index into the array of link objects.
<script language="JavaScript"><-- Hide // ******************************************************** // To Use this Script, simply insert the Descriptions and // URLs below as indicated. // // That's all there is; no code to change. // // NOTE: use a \ before any ' or " characters that appear // in your descriptions // ******************************************************** // ***************************************** // Definition of our link object and methods // ***************************************** function link(description,URL) { this.description=description; this.URL=URL } function linkToString() { return '<A HREF="'+this.URL+'">'+this.description+'</A><BR>' } link.prototype.toString = linkToString; // ************************************** // Definition of an Array of link objects // ************************************** function linkArray() { var a = linkArray.arguments; // Get the arguments passed if (a.length%2!=0) { // Make sure the number of arguments is even alert("Can not initialize Link Array: Incorrect number of strings"); this.length=0; } else { // Loop thru the argments creating link objects for (ti=ai=0;ai<a.length;ti++,ai+=2) { this[ti]=new link(a[ai],a[ai+1]); } this.length=a.length/2; } } // ************************************************************ // function to create a random order array of a specifed length // ************************************************************ var RandomValue = new Array(); function buildArrayofRandomValues(length) { for (i=0; i<length; i++) { RandomValue[i]=-1; // initialize value for loop while(RandomValue[i]==-1) { // Loop until a value is assigned r=Math.floor(Math.random()*length); // Pick a random number for(j=0; j<=i && RandomValue[j]!=r; j++) { // See if we've picked this number already if (j==i) RandomValue[i]=r; } } } } var URLlist=new linkArray( // ***************************************************** // Place URLs and Descriptions Here, Separated by Commas // NOTE: use a \ before any ' or " characters that appear // in your descriptions // ***************************************************** "Javascripts.com", "http://www.javascripts.com", "Disney", "http://www.disney.com", "The Dilbert Zone", "http://www.unitedmedia.com/comics/dilbert/", "This is True", "http://www.thisistrue.com/", "Centre for The Easily Amused", "http://www.amused.com/" // ******************************* // !NOTE: No Comma after last URL! // ******************************* ); // *************************** // this code displays the list // *************************** buildArrayofRandomValues(URLlist.length); for (i=0; i<URLlist.length; i++) { document.write(URLlist[RandomValue[i]]); }; // --></SCRIPT> <BR>Or imbed the links (<script language="JavaScript"><!-- document.write(URLlist[randomIndex()]); //--></SCRIPT>) in various places in your page. Here's another link: <script language="JavaScript"><!-- document.write(URLlist[randomIndex()]); //--></SCRIPT>. Another link is: <script language="JavaScript"><!-- document.write(URLlist[randomIndex()]); //--></SCRIPT>. The number of links (<script language="JavaScript"><!-- document.write(URLlist[randomIndex()]); //--></SCRIPT>) does not have to be the same as the number of references.